package application.common.cron4j;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.cron4j.ITask;
import application.common.model.User;
import application.common.render.HttpClientResult;
import application.common.util.HttpUtil;
import application.common.util.ShiroUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class UserTask implements ITask{
	
	private boolean isEnd=true;
	
	public void run() {
		if(isEnd) {
			if(ShiroUtil.getConfig()!=null && !StrKit.isBlank(ShiroUtil.getConfig().getSynURL())) {
				isEnd=false;
				try {
					int pageNumber=1;
					StringBuffer sb=new StringBuffer();
					while(true) {
						HttpClientResult httpClientResult=HttpUtil.doGet(String.format("%s/api/cab/getUsers?cabinetId=%s&pageNumber=%s", ShiroUtil.getConfig().getSynURL(),ShiroUtil.getAuth(),pageNumber));
						if(httpClientResult==null) {
							break;
						}
						if(httpClientResult.getCode()!=200) {
							break;
						}
						String content=httpClientResult.getContent();
						if(StrKit.isBlank(content)) {
							break;
						}
						JSONObject jsonObject=JSONObject.fromObject(content);
						if(jsonObject==null) {
							break;	
						}
						if(jsonObject.has("code")) {
							int code=jsonObject.getInt("code");
							if(code!=200) {
								break;
							}
							if(jsonObject.has("data")) {
								String data=jsonObject.getString("data");
								if(StrKit.isBlank(data)) {
									break;
								}
								JSONArray array=JSONArray.fromObject(data);
								if(array==null || array.size()==0) {
									break;
								}
								
								for(Object object:array) {
									JSONObject row=JSONObject.fromObject(object);
									if(row!=null) {
										String id=row.getString("ID");
										User user=User.dao.findById(id);
										if(user==null) {
											user=new User();
										}
										if(StrKit.equals("2", row.getString("STATE"))) {
											if(!StrKit.isBlank(user.getId())) {
												user.delete();
											}
											return;
										}
										
										if(!StrKit.equals(user.getName(), row.getString("NAME"))) {
											user.setName(row.getString("NAME"));
										}
										if(!StrKit.equals(user.getUsername(), row.getString("USERNAME"))) {
											user.setUsername(row.getString("USERNAME"));
										}
										if(!StrKit.equals(user.getZp(), row.getString("PHOTO"))) {
											user.setZp(row.getString("PHOTO"));
										}
										if(!StrKit.equals(user.getPassword(), row.getString("PASSWORD"))) {
											user.setPassword(row.getString("PASSWORD"));
										}
										if(row.containsKey("roleKey")) {
											user.setRoleKey(row.getString("roleKey"));
										}
										if(row.containsKey("roleName")) {
											user.setRoleName(row.getString("roleName"));
										}
										
										if(row.containsKey("fpr1") && !StrKit.equals("null", row.getString("fpr1")) && !StrKit.isBlank( row.getString("fpr1"))
												) {
											user.setFpr1(row.getString("fpr1"));
										}
										if(row.containsKey("fpr2")&& !StrKit.equals("null", row.getString("fpr2")) && !StrKit.isBlank( row.getString("fpr2"))) {
											user.setFpr2(row.getString("fpr2"));
										}
										
										if(StrKit.isBlank(user.getId())) {
											user.setId(id);
											user.save();
										}
										else {
											user.update();
										}
										sb.append("'").append(user.getId()).append("'").append(",");
									}
								}
								pageNumber++;
							}else {
								break;
							}
						}else {
							break;
						}						
					}
					if(!StrKit.isBlank(sb.toString())) {
						Db.delete("delete from "+User.TABLE_NAME+" where id not in ("+(sb.substring(0,sb.length()-1).toString())+") "); 
					}
					isEnd=true;
				}catch (Exception e) {
					isEnd=true;
				}
			}
			isEnd=true;
		}
	}

	public void stop() {
	}

}
